package com.zhixinhuixue.zsyte.xxx.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.scandecode.ScanDecode
import com.scandecode.inf.ScanInterface
import com.scandecode.inf.ScanInterface.OnScanListener
import com.zhixinhuixue.library.common.ext.dp
import com.zhixinhuixue.zsyte.xxx.R
import com.zhixinhuixue.zsyte.xxx.app.api.NetUrl
import com.zhixinhuixue.zsyte.xxx.app.base.BaseActivity
import com.zhixinhuixue.zsyte.xxx.app.ext.initBack
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityJwwlcxBinding
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityWlckBinding
import com.zhixinhuixue.zsyte.xxx.ui.adapter.JWWLCXAdapter
import com.zhixinhuixue.zsyte.xxx.ui.adapter.WLPBAdapter
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.JWWLCXViewModel
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.WLCKViewModel
import me.hgj.mvvmhelper.ext.*
import me.hgj.mvvmhelper.net.LoadStatusEntity
import me.hgj.mvvmhelper.net.LoadingDialogEntity
import me.hgj.mvvmhelper.util.decoration.DividerOrientation
import java.util.*


/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　: 虽然在Activity代码少了，但是DataBinding 不太好用
 */
class JWWLCXActivity : BaseActivity<JWWLCXViewModel, ActivityJwwlcxBinding>() {

    private var scanDecode: ScanInterface? = null
    private val testAdapter: JWWLCXAdapter by lazy { JWWLCXAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        //初始化toolbar
        mToolbar.initBack("架位物料查询") {
            finish()
        }


        //初始化recyclerview
        mDataBind.listRecyclerView.run {
            vertical()
            divider {
                setColor(getColorExt(R.color.colorDivider))
                setDivider(1.dp)
                includeVisible = false
                orientation = DividerOrientation.VERTICAL
            }
            adapter = testAdapter
        }


        initScanner()
    }

    private fun initScanner() {
        try {
            scanDecode = ScanDecode(this)
            scanDecode?.initService("true")
            scanDecode?.getBarCode(object : OnScanListener {
                override fun getBarcode(data: String) {
                    if (!TextUtils.isEmpty(data)) {
                        mDataBind.edtWlpb.setText(data)
                        mDataBind.edtWlpb.clearFocus()
                        mDataBind.edtWlpb.requestFocus()
                        mViewModel.getMaterialShelvesById(data)
                    }
                }

                override fun getBarcodeByte(bytes: ByteArray) {}
            })
        } catch (e: Exception) {

        }


    }


    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
        //监听登录结果
        mViewModel.itemData.observe(this, {
            testAdapter.setList(it)
        })


    }

    /**
     * 请求失败
     * @param loadStatus LoadStatusEntity
     */
    override fun onRequestError(loadStatus: LoadStatusEntity) {
        when (loadStatus.requestCode) {
            NetUrl.GET_MATERIALSHELVES -> {
                showDialogMessage(loadStatus.errorMessage)
            }

        }
    }

    inner class LoginClickProxy {

    }

    override fun showCustomLoading(setting: LoadingDialogEntity) {
        if (setting.requestCode == NetUrl.LOGIN) {
            //可以根据不同的code 做不同的loading处理
            showLoadingUi()
        }
    }

    override fun dismissCustomLoading(setting: LoadingDialogEntity) {
        if (setting.requestCode == NetUrl.LOGIN) {
            //可以根据不同的code 做不同的loading处理
            showSuccessUi()
        }
    }

    override fun onDestroy() {

        try {
            scanDecode!!.stopScan()
            scanDecode!!.onDestroy()

        } catch (e: Exception) {

        }
        super.onDestroy()
    }


}